package baltazar;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


public class ListArea extends JPanel{
	
	private String[] colNames = { "ID", "Imię", "Nazwisko", "Miejsce zam.", "Nr. telefonu" };
	String data[][] = {{"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
   {"001","vinod","Bihar","India","Biology","65","First"},
   {"002","Raju","ABC","Kanada","Geography","58","second"},
   {"003","Aman","Delhi","India","computer","98","Dictontion"},
   {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"}};
	
	private JTable table = new JTable(data, colNames);
	private JScrollPane scrollPane = new JScrollPane(table);

	private TableRowSorter<TableModel> sorter;

	public ListArea() {
		setLayout(new BorderLayout());
		
		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

		sorter = new TableRowSorter<TableModel>(table.getModel());
		table.setRowSorter(sorter);
		scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		scrollPane.setBackground(new Color(250, 250, 198));
		table.setGridColor(new Color(250, 250, 198));
		table.getTableHeader().setBackground(new Color(250, 250, 198));
		table.setBackground(new Color(230, 145, 56));
		
		add(scrollPane, BorderLayout.CENTER);
	}
	
	public int[] getColumnsWidthArray() {
		int[] array = new int[table.getColumnCount()];
		for(int i=0; i<table.getColumnCount(); i++) {
			array[i] = table.getColumnModel().getColumn(i).getWidth();
		}
		return array;
	}
	
	public void updateFilter(final JTextField tf) {
		tf.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void insertUpdate(DocumentEvent e) {
				filter(tf);
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				filter(tf);
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				filter(tf);
			}
		});
	}
	
	private void filter(JTextField tf) {
		RowFilter<TableModel, Object> rf = null;
        try {
            rf = RowFilter.regexFilter("^"+tf.getText(), 1);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
	}
	
}
